package com.dayatang.hrm.compensation.infra.hibernate;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry5.hibernate.HibernateSessionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.dayatang.commons.repository.EntityRepositoryHibernateTapestry;
import com.dayatang.hrm.compensation.domain.SalaryAccountSuite;
import com.dayatang.hrm.compensation.domain.SalaryCalculateItem;
import com.dayatang.hrm.compensation.domain.SalaryCalculateItemSetting;
import com.dayatang.hrm.compensation.domain.SalaryCalculateItemSettingRepository;
import com.dayatang.hrm.compensation.domain.SalaryItem;
import com.dayatang.hrm.compensation.domain.SalaryItemSetting;

@SuppressWarnings("unchecked")
public class SalaryCalculateItemSettingRepositoryImpl extends
		EntityRepositoryHibernateTapestry<SalaryCalculateItemSetting, Long>
		implements SalaryCalculateItemSettingRepository {

	private static final Logger logger = LoggerFactory
			.getLogger(SalaryAccountSuiteSettingRepositoryImpl.class);

	public SalaryCalculateItemSettingRepositoryImpl(
			HibernateSessionManager sessionManager) {
		super(SalaryCalculateItemSetting.class, sessionManager);
	}

	@Override
	public SalaryCalculateItemSetting get(SalaryCalculateItem salaryCalItem,
			Date queryDate) {
		List<SalaryCalculateItemSetting> list = getSessionManager()
				.getSession().getNamedQuery("SalaryCalculateItemSetting.get")
				.setEntity("pCalItem", salaryCalItem).setTimestamp(
						"pQueryDate", queryDate).list();

		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SalaryItemSetting get(SalaryItem salaryItem, Date queryDate) {
		List<SalaryItemSetting> list = getSessionManager().getSession()
				.getNamedQuery("SalaryItemSetting.get").setEntity("pItem",
						salaryItem).setTimestamp("pQueryDate", queryDate)
				.list();

		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public SalaryCalculateItemSetting getPure(
			SalaryCalculateItemSetting salaryCalculateItemSetting) {
		getSessionManager().getSession().evict(salaryCalculateItemSetting);
		return get(salaryCalculateItemSetting.getId());
	}

	@Override
	public boolean isNameAlreadyUsedInSameSalaryAccountSuiteExceptSelf(
			String name, SalaryAccountSuite salaryAccountSuite, Date fromDate,
			Date toDate, SalaryCalculateItemSetting selfSetting) {

		boolean isDuplicate = false;
		String comparedName = name.trim();
		List<SalaryCalculateItemSetting> settings = new ArrayList<SalaryCalculateItemSetting>();

		if (selfSetting == null) {
			settings = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryCalculateItemSetting.isNameAlreadyUsedInSameSalaryAccountSuiteExceptSelf.selfIsNull")
					.setEntity("pSuite", salaryAccountSuite).setTimestamp(
							"pFromDate", fromDate).setTimestamp("pToDate",
							toDate).list();
		} else {
			settings = getSessionManager()
					.getSession()
					.getNamedQuery(
							"SalaryCalculateItemSetting.isNameAlreadyUsedInSameSalaryAccountSuiteExceptSelf.selfIsExist")
					.setEntity("pSuite", salaryAccountSuite).setEntity("pSelf",
							selfSetting).setTimestamp("pFromDate", fromDate)
					.setTimestamp("pToDate", toDate).list();
		}

		if (settings.isEmpty()) {
			return isDuplicate;
		}

		for (SalaryCalculateItemSetting setting : settings) {

			if (logger.isDebugEnabled()) {
				logger
						.debug(
								"薪资帐套【{}】在时间段【{}至 {}】有效的薪资计算项目设置信息为：name = {}， fromDate = {}，toDate = {}",
								new Object[] { salaryAccountSuite.getName(),
										fromDate, toDate, setting.getName(),
										setting.getFromDate(),
										setting.getToDate() });
			}

			String existedName = setting.getName();

			isDuplicate = existedName.equals(comparedName);

			if (isDuplicate)
				return isDuplicate;

		}

		return isDuplicate;

	}
}
